package com.moody.service.impl;

import java.util.List;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.moody.hibernate.dao.CustAddressDAO;
import com.moody.hibernate.dao.CustLimitDAO;
import com.moody.hibernate.dao.DebtEcDAO;
import com.moody.hibernate.dao.PriceRarocDAO;
import com.moody.hibernate.domain.CustLimit;
import com.moody.hibernate.domain.DebtEc;
import com.moody.hibernate.domain.PriceRaroc;
import com.moody.service.RiskAppService;

/*
 * Copyright © Moody analytics
 *
 * @Author: Wang Fuqiang
 * @Time: 2011-12-24 下午04:44:49
 * @Modified by: Wangfuqiang 2011-12-24
 * @Description: 
 */

@Service
public class RiskAppServiceImpl implements RiskAppService {
	private static final Logger log = LoggerFactory.getLogger(RiskAppServiceImpl.class);
	WebApplicationContext web = ContextLoader.getCurrentWebApplicationContext();
	ServletContext context = web.getServletContext();  
	WebApplicationContext ctx  = WebApplicationContextUtils.getWebApplicationContext(context);
	
	private CustLimitDAO custLimitDAO = CustLimitDAO.getFromApplicationContext(ctx);
	private DebtEcDAO debtEcDAO = DebtEcDAO.getFromApplicationContext(ctx);
	private PriceRarocDAO priceRarocDAO = PriceRarocDAO.getFromApplicationContext(ctx);
	
	public CustLimit findCustLimitByCustId(String custId) {
		List<CustLimit> list = custLimitDAO.findByCustId(custId);
		
		if(list.size() > 0 )
			return list.get(0);
		else
			return null;
	}

	public void insertCustLimit(CustLimit custLimit) {
		custLimitDAO.save(custLimit);
	}

	public void updateCustLimit(CustLimit custLimit) {
		custLimitDAO.update(custLimit);
	}

	public DebtEc findDebtECByDebtNo(String debtNo) {
		List<DebtEc> list = debtEcDAO.findByDebtNo(debtNo);
		if(list.size()>0)
			return list.get(0);
		else
			return null;
	}

	public void insertDebtEC(DebtEc debtEc) {
		debtEcDAO.save(debtEc);
	}

	public void updateDebtEC(DebtEc debtEc) {
		debtEcDAO.update(debtEc);
	}

	public PriceRaroc findPriceRarocByDebtNo(String debtNo) {
		List<PriceRaroc> list = priceRarocDAO.findByDebtNo(debtNo);
		if(list.size()>0)
			return list.get(0);
		else
			return null;
	}

	public void updatePriceRaroc(PriceRaroc priceRaroc) {
		priceRarocDAO.update(priceRaroc);
	}

	public void insertPriceRaroc(PriceRaroc priceRaroc) {
		priceRarocDAO.save(priceRaroc);
	}	
}
